package com.schoolcardinfo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.schoolcardinfo.beans.ExpenseInfo;
import com.schoolcardinfo.biz.IExpenseInfoBiz;
import com.schoolcardinfo.biz.impl.ExpenseInfoBizImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ExpenseInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PrintWriter out;
	private HttpSession session;
	IExpenseInfoBiz expenseInfoBiz = new ExpenseInfoBizImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		session=request.getSession();
		out=response.getWriter();
		
		if("getConsume".equals(op)) {
			getConsume(request, response);
		} else if("getAllConsume".equals(op)) {
			getAllConsume(request, response);
		}
	}

	//通过卡号获取这张卡的消费记录
	private void getConsume(HttpServletRequest request, HttpServletResponse response) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		List<ExpenseInfo> list = expenseInfoBiz.selectExpenseInfo(sid);
		if(list!=null && list.size()>0) {
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
		} else {
			out.print(0);
		}
		out.flush();
		out.close();
	}
	
	//查询所有的消费记录
	private void getAllConsume(HttpServletRequest request, HttpServletResponse response) {
		List<ExpenseInfo> list = expenseInfoBiz.selectExpenseInfo(0);
		if(list!=null && list.size()>0) {
			JSONArray json=JSONArray.fromObject(list);
			JSONObject jb=new JSONObject();
			jb.put("rows",json);
			out.print(jb.toString());
			out.flush();
			out.close();
		}
	}

}
